package com.shengdong.water.code.ext

import android.widget.ImageView
import coil.load
import coil.transform.CircleCropTransformation
import coil.transform.RoundedCornersTransformation

/**
 * 使用 Coil 加载图片的扩展函数
 * @param url 图片URL
 * @param placeholder 占位图资源ID
 * @param error 错误图资源ID
 */
fun ImageView.loadImage(
    url: String?,
    placeholder: Int? = null,
    error: Int? = null
) {
    this.load(url) {
        crossfade(true)
        placeholder?.let { placeholder(it) }
        error?.let { error(it) }
    }
}

/**
 * 加载圆形图片
 */
fun ImageView.loadCircleImage(
    url: String?,
    placeholder: Int? = null,
    error: Int? = null
) {
    this.load(url) {
        crossfade(true)
        transformations(CircleCropTransformation())
        placeholder?.let { placeholder(it) }
        error?.let { error(it) }
    }
}

/**
 * 加载圆角图片
 * @param radius 圆角半径（像素）
 */
fun ImageView.loadRoundedImage(
    url: String?,
    radius: Float,
    placeholder: Int? = null,
    error: Int? = null
) {
    this.load(url) {
        crossfade(true)
        transformations(RoundedCornersTransformation(radius))
        placeholder?.let { placeholder(it) }
        error?.let { error(it) }
    }
}